#include "triggermanager.h"

#include <QMenu>
#include <QDebug>

#include "actionmanager.h"
#include "locationandsizetoolbar.h"
#include "shapeformattingtoolbar.h"
#include "textformattingtoolbar.h"
#include "toolbarmanager.h"
#include "menumanager.h"
#include "svgnode.h"

void TriggerManager::createMenubarAndToolBar(QMainWindow *parent)
{
    if(!parent)
        return;

    m_ActionManager = new ActionManager();
    m_ToolbarManager  = new ToolbarManager(m_ActionManager);
    m_MenuManager = new MenuManager(m_ActionManager);

    if(m_ActionManager && m_ToolbarManager && m_MenuManager)
    {
        m_ActionManager->createActions(parent);
        m_ToolbarManager->createToolbars(parent);
        m_MenuManager->createMenubar(parent);

        // toolbars visible
        connect(m_MenuManager->m_MenuToolbars,SIGNAL(triggered(QAction*)),m_ToolbarManager,SLOT(slotSetToolbarsVisible(QAction*)));
        // grid setting

    }
}

void TriggerManager::slotUpdateActionEnableState(QList<QGraphicsItem*> list)
{
	int itemCnt = list.count();
    m_ActionManager->m_ActionCut->setEnabled((bool)itemCnt);
    m_ActionManager->m_ActionCopy->setEnabled((bool)itemCnt);
    m_ActionManager->m_ActionPaste->setEnabled(true);
    m_ActionManager->m_ActionDelete->setEnabled((bool)itemCnt);

    bool itemGroup = false;
    if(itemCnt >= 2)
    {
        itemGroup = true;
    }

    m_ActionManager->m_ActionGroup->setEnabled(itemGroup);

	bool isUngroup = false;
	if (itemCnt == 1)
	{
		SvgNode *node = dynamic_cast<SvgNode*>(list.first());
		if (node && node->svgType() == SvgNode::SVG_GROUP)
		{
			isUngroup = true;
		}
	}
	m_ActionManager->m_ActionUngroup->setEnabled(isUngroup);

    m_ActionManager->m_ActionAlignLeft->setEnabled(itemGroup);
    m_ActionManager->m_ActionAlignCenter->setEnabled(itemGroup);
    m_ActionManager->m_ActionAlignRight->setEnabled(itemGroup);
    m_ActionManager->m_ActionAlignTop->setEnabled(itemGroup);
    m_ActionManager->m_ActionAlignMiddle->setEnabled(itemGroup);
    m_ActionManager->m_ActionAlignBottom->setEnabled(itemGroup);

    if(itemCnt ==0)
    {
        m_ActionManager->m_ActionDistributeH->setEnabled(false);
        m_ActionManager->m_ActionDistributeV->setEnabled(false);
    }
    else if(itemCnt >= 3)
    {
        m_ActionManager->m_ActionDistributeH->setEnabled(true);
        m_ActionManager->m_ActionDistributeV->setEnabled(true);
    }

    //
    m_ToolbarManager->m_LocationAndSizeToolBar->setEnabled((bool)itemCnt);
    m_ToolbarManager->m_ShapeFormattingToolBar->setEnabled((bool)itemCnt);
    //m_ToolbarManager->m_TextFormattingToolBar->setEnabled((bool)itemCnt);
}

void TriggerManager::slotUpateLocationAndSize(QRectF rect)
{
    m_ToolbarManager->m_LocationAndSizeToolBar->setLocationAndSize(rect);
}

void TriggerManager::slotUpdateTextToolbarEnableState(bool enable)
{
	m_ToolbarManager->m_TextFormattingToolBar->setEnabled(enable);
}

TriggerManager::TriggerManager(QObject *parent) :
    QObject(parent)
{
    m_ActionManager = NULL;
    m_ToolbarManager = NULL;
    m_MenuManager = NULL;
}

TriggerManager::~TriggerManager()
{
    if(m_ActionManager)
    {
        delete m_ActionManager;
        m_ActionManager = NULL;
    }
    if(m_ToolbarManager)
    {
        delete m_ToolbarManager;
        m_ToolbarManager = NULL;
    }
    if(m_MenuManager)
    {
        delete m_MenuManager;
        m_MenuManager = NULL;
    }
}

